/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package service.impl;

import eao.EaoLocal;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import model.entity.Resource;
 
import service.InterfaceServiceLocal;
import service.ResourceServiceLocal;

/**
 *
 * @author dingnangen
 */
@Stateless

public class ResourceManager extends AbstractService<Resource> implements InterfaceServiceLocal<Resource>,ResourceServiceLocal {

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @EJB(beanName = "ResourceFacade")
    eao.EaoLocal<Resource> resourceFacade;

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @Override
    protected EaoLocal getEao() {
        return resourceFacade;
    }
    
    /**
     *
     * @param userid
     * @param nodeDepth
     * @param parentNodeId
     * @param nodeType
     * @param available
     * @return
     */
    @Override
    public List<Resource> getResourcesbyUserid(int userid, int nodeDepth, int parentNodeId, int nodeType, boolean available) {
        ArrayList params = new ArrayList();
        int count = 1;
        String queryStr = "select DISTINCT rr.idresource from RoleResource rr where rr.idrole.userList in "
                + "(select u from User u where u.iduser=?"+(count++)+" ) ";
        params.add(userid);
        if (nodeDepth != -1) {
            queryStr += " and rr.idresource.nodeDepth=?"+(count++);
            params.add(nodeDepth);
        }
        if (parentNodeId != -1) {
            queryStr += " and rr.idresource.parentNodeId=?"+(count++);
            params.add(parentNodeId);
        }
        if (nodeType != -1) {
            queryStr += " and rr.idresource.nodeType=?"+(count++);
            params.add(nodeType);
        }

        queryStr += " and rr.idresource.available=?"+(count++)+" order by rr.idresource.resourceorder";
        params.add(available);

       // this.findAll(queryStr , params);
        System.out.println("queryStr:"+queryStr);
        System.out.println("params:"+params);
        return this.findAll(queryStr, params);
    }

    /**
     *
     * @param nodeDepth
     * @param parentNodeId
     * @param nodeType
     * @param available
     * @return
     */
    @Override
    public List<Resource> getResourcesbyParentNodeId(int nodeDepth, int parentNodeId, int nodeType, boolean available) {
        ArrayList params = new ArrayList();
        int count = 1;
        String queryStr = "select DISTINCT r from Resource r where ";
        
        if (nodeDepth != -1) {
            queryStr += " r.nodeDepth=?"+(count++);
            params.add(nodeDepth);
        }
        if (parentNodeId != -1) {
            queryStr += " and r.parentNodeId=?"+(count++);
            params.add(parentNodeId);
        }
        if (nodeType != -1) {
            queryStr += " and r.nodeType=?"+(count++);
            params.add(nodeType);
        }

        queryStr += " and r.available=?"+(count++)+" order by r.resourceorder";
        params.add(available);

       
        return this.findAll(queryStr, params);
    }

    @Override
    public List<Resource> getResourcesbyLoginname(String loginname, int nodeDepth, int parentNodeId, int nodeType, boolean available) {
        ArrayList params = new ArrayList();
        int count = 1;
        String queryStr = "select DISTINCT rr.idresource from RoleResource rr where rr.idrole.userList in "
                + "(select u from User u where u.loginname=?"+(count++)+" ) ";
        params.add(loginname);
        if (nodeDepth != -1) {
            queryStr += " and rr.idresource.nodeDepth=?"+(count++);
            params.add(nodeDepth);
        }
        if (parentNodeId != -1) {
            queryStr += " and rr.idresource.parentNodeId=?"+(count++);
            params.add(parentNodeId);
        }
        if (nodeType != -1) {
            queryStr += " and rr.idresource.nodeType=?"+(count++);
            params.add(nodeType);
        }

        queryStr += " and rr.idresource.available=?"+(count++)+" order by rr.idresource.resourceorder";
        params.add(available);

       // this.findAll(queryStr , params);
        System.out.println("queryStr:"+queryStr);
        System.out.println("params:"+params);
        return this.findAll(queryStr, params);
    }
    
    
    
}
